package com.vfa.vocaburay;

import java.util.ArrayList;

import com.vfa.data.Language;
import com.vfa.test.CatagoryE;
import com.vfa.test.QuestionData;
import com.vfa.test.QuestionE;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class TestOption extends Activity {
	
	public ArrayList<QuestionE> listQuestion;
	public ArrayList<CatagoryE> catalogies;
	QuestionData questiondata;
	EditText username, numberquestion;
	Button startTest, cancelTest;
	Spinner catalogy;
	int question = 0;
	ArrayList<String> arrlistcata;
	ArrayList<CatagoryE> arraylistcatagory;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_option);
		
		username = (EditText) findViewById(R.id.editUsername);
		numberquestion = (EditText) findViewById(R.id.editNumberQuestion);
		startTest = (Button) findViewById(R.id.startTest);
		cancelTest = (Button) findViewById(R.id.cancelTest);
		catalogy = (Spinner) findViewById(R.id.spinner_level);
		questiondata = new QuestionData(this);
		listQuestion = QuestionData.getQuestion(1);
		
		arraylistcatagory = QuestionData.getAllCatagory();
		ArrayAdapter<CatagoryE> levelAdapter = new ArrayAdapter<CatagoryE>(this, R.layout.listiterm, arraylistcatagory);
		levelAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		catalogy.setAdapter(levelAdapter);
		
			
		startTest.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if((username.getText().toString().equals(""))||(numberquestion.getText().toString().equals(""))){
					Toast.makeText(getBaseContext(), "Please fill username and number question", Toast.LENGTH_SHORT).show();
				}
				else{
					question = Integer.parseInt(numberquestion.getText().toString());
					if(question > listQuestion.size()){
					Toast.makeText(getBaseContext(), "Number question in database not enough", Toast.LENGTH_SHORT).show();
				}
				else{
					Intent intent = new Intent(TestOption.this, actTestExam.class);
					intent.putExtra("userName", username.getText().toString())
						.putExtra("Numberquestion", question);
					startActivity(intent);
					TestOption.this.finish();
				}
				
			}
			}
		});
		
		cancelTest.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AlertDialog.Builder bd = new AlertDialog.Builder(TestOption.this);
				bd.setMessage("Are you want to exit?").setCancelable(false)
				.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
			            TestOption.this.finish();		
					}
				});
				bd.setNegativeButton("No", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
						
					}
				}).show();
				
			}
		});
	}
	
}
